package cn.iocoder.yudao.module.blog.controller.admin.report;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.report.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.report.BlogReportDO;
import cn.iocoder.yudao.module.blog.convert.report.BlogReportConvert;
import cn.iocoder.yudao.module.blog.service.report.BlogReportService;

@Tag(name = "管理后台 - 上报统计")
@RestController
@RequestMapping("/blog/report")
@Validated
public class BlogReportController {

    @Resource
    private BlogReportService reportService;

    @PostMapping("/create")
    @Operation(summary = "创建上报统计")
    @PreAuthorize("@ss.hasPermission('blog:report:create')")
    public CommonResult<Long> createReport(@Valid @RequestBody BlogReportCreateReqVO createReqVO) {
        return success(reportService.createReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新上报统计")
    @PreAuthorize("@ss.hasPermission('blog:report:update')")
    public CommonResult<Boolean> updateReport(@Valid @RequestBody BlogReportUpdateReqVO updateReqVO) {
        reportService.updateReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除上报统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:report:delete')")
    public CommonResult<Boolean> deleteReport(@RequestParam("id") Long id) {
        reportService.deleteReport(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得上报统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:report:query')")
    public CommonResult<BlogReportRespVO> getReport(@RequestParam("id") Long id) {
        BlogReportDO report = reportService.getReport(id);
        return success(BlogReportConvert.INSTANCE.convert(report));
    }

    @GetMapping("/list")
    @Operation(summary = "获得上报统计列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:report:query')")
    public CommonResult<List<BlogReportRespVO>> getReportList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogReportDO> list = reportService.getReportList(ids);
        return success(BlogReportConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得上报统计分页")
    @PreAuthorize("@ss.hasPermission('blog:report:query')")
    public CommonResult<PageResult<BlogReportRespVO>> getReportPage(@Valid BlogReportPageReqVO pageVO) {
        PageResult<BlogReportDO> pageResult = reportService.getReportPage(pageVO);
        return success(BlogReportConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出上报统计 Excel")
    @PreAuthorize("@ss.hasPermission('blog:report:export')")
    @OperateLog(type = EXPORT)
    public void exportReportExcel(@Valid BlogReportExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogReportDO> list = reportService.getReportList(exportReqVO);
        // 导出 Excel
        List<BlogReportExcelVO> datas = BlogReportConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "上报统计.xls", "数据", BlogReportExcelVO.class, datas);
    }

}
